package com.yunyan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyan.entity.LoveData;
import com.yunyan.entity.SchoolData;
import com.yunyan.resp.JsonResp;
import com.yunyan.service.LoveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/love")
public class LoveController {



    @Resource
    private LoveService loveService;

    @PostMapping
    public JsonResp addLove(@RequestBody LoveData loveData){
        List<SchoolData> schoolDataList = loveService.listLove(loveData.getUserId());
        if (schoolDataList.isEmpty()) {
            return JsonResp.ok(loveService.save(loveData));
        }

        List<String> collect = schoolDataList.stream().map(SchoolData::getId).collect(Collectors.toList());
        if (collect.contains(loveData.getSchoolId())) {
            return JsonResp.error();
        }
        return JsonResp.ok(loveService.save(loveData));
    }

    @GetMapping("/{userId}")
    public JsonResp listLove(@PathVariable String userId) {
        return JsonResp.ok(loveService.listLove(userId));
    }

    @DeleteMapping("/{schoolId}/{userId}")
    public JsonResp deleteLove(@PathVariable String schoolId, @PathVariable String userId){
        return JsonResp.ok(loveService.remove(new LambdaQueryWrapper<LoveData>()
                        .eq(LoveData::getSchoolId, schoolId)
                        .eq(LoveData::getUserId, userId)
                )
        );
    }
}
